import time

from common.basepage import Basepage
from selenium import webdriver
from selenium.webdriver.common.by import By
from common.yaml_handler import result
Host = result["Host"]


class OrganizationPage(Basepage):
    url = Host + "/#/project/organization"
    """管理元素定位"""
    # 新增按钮
    add = (By.XPATH, "//i[@class='el-icon-plus']")
    # 一级部门名称
    department_name = (By.XPATH, "//input[@placeholder='请输入部门名称，2~30字以内']")
    # 二级部门名称
    subordinate_department_name = (By.XPATH, "//input[@placeholder='请输入部门名称,2-30字']")
    # 排序
    sort = (By.XPATH, "//div[@class='el-form-item']//input[@class='el-input__inner']")
    # 确定按钮
    determine = (By.XPATH, "//div[@class='rightbutton']")
    # 取消按钮
    cancel = (By.XPATH, "//div[@class='dialog-footer']")
    # 修改按钮（第一条数据）
    edit = (By.XPATH, "//tr[@class= 'el-table__row el-table__row--striped el-table__row--level-1']//div[@class='icon_edit_buton']")
    # 修改时的部门名称弹窗
    edit_department_name = (By.XPATH, "//label[contains(text(), '部门名称')]//following-sibling::div//input")
    # 删除按钮（第一条数据）
    delete = (By.XPATH, "//tr[2]//td[3]//div[1]//div[1]//div[4]")
    # 删除弹框-确认按钮
    delete_confirm = (By.XPATH, "//button[@class = 'ok-btn']")
    # 删除成功的提示弹窗
    delete_sucsses_notification = (By.XPATH, "//p[@class = 'el-message__content']")
    # 新增下级按钮（第一条数据）
    add_subordinate = (By.XPATH, "//tr[2]//td[3]//div[1]//div[1]//div[1]")
    # 有子部门下删除部门的弹窗信息
    delete_failed_notification = (By.XPATH, "//p[contains(text(), '该部门还有子部门，不允许删除')]")
    # 点开第一条数据的下级
    open_subordinate = (By.XPATH, "//tr[2]//div//i")
    # 删除二级部门“第一部门分部”
    delete_subordinate = (By.XPATH, "//tr[3]//td[3]//div[1]//div[1]//div[4]")

    def add_department(self, name, numb="1"):
        self.click(self.add)
        self.fill(self.department_name, name)
        time.sleep(2)
        self.fill(self.sort, numb)
        self.click(self.determine)
        el = self.driver.find_element(By.XPATH, f"//div[contains(text(), '{name}')]")
        text = el.text
        return text

    def add_department_error_information(self):
        el = self.driver.find_element(By.CLASS_NAME, 'el-form-item__error')
        text = el.text
        return text

    def edit_department(self, name):
        self.click(self.edit)
        self.clear(self.edit_department_name)
        self.fill(self.edit_department_name, name)
        time.sleep(2)
        self.click(self.determine)
        el = self.driver.find_element(By.XPATH, f"//div[contains(text(), '{name}')]")
        text = el.text
        return text

    def delete_department_sucsses(self, name, numb="1"):
        self.click(self.add)
        self.fill(self.department_name, name)
        time.sleep(2)
        self.fill(self.sort, numb)
        self.click(self.determine)
        time.sleep(2)
        self.click(self.delete)
        time.sleep(2)
        self.click(self.delete_confirm)
        elem = self.driver.find_element(*self.delete_sucsses_notification)
        text = elem.text
        return text

    def delte_department_fail(self, name, numb="1", subordinate_name="第一部门分部", subordinate_numb="1"):
        self.click(self.add)
        self.fill(self.department_name, name)
        time.sleep(2)
        self.fill(self.sort, numb)
        self.click(self.determine)
        time.sleep(2)
        # 点击新增下级
        self.click(self.add_subordinate)
        self.fill(self.subordinate_department_name, subordinate_name)
        time.sleep(1)
        self.fill(self.sort, subordinate_numb)
        time.sleep(1)
        # 点击保存
        self.click(self.determine)
        time.sleep(1)
        # 点击删除
        self.click(self.delete)
        time.sleep(1)
        self.click(self.delete_confirm)
        # 获取弹窗提示信息做第一个断言,"该部门还有子部门，不允许删除"
        elem = self.driver.find_element(*self.delete_failed_notification)
        text = elem.text
        return text

    def delte_subordinate_department_sucsses(self):
        self.click(self.open_subordinate)
        time.sleep(1)
        self.click(self.delete_subordinate)
        time.sleep(1)
        self.click(self.delete_confirm)
        time.sleep(1)
        self.click(self.delete)
        time.sleep(2)
        self.click(self.delete_confirm)
        time.sleep(2)
        elem = self.driver.find_element(*self.delete_sucsses_notification)
        text = elem.text
        return text

    def clear_data(self):
        self.click(self.delete)
        time.sleep(2)
        self.click(self.delete_confirm)
        time.sleep(1)
        elem = self.driver.find_element(*self.delete_sucsses_notification)
        text = elem.text
        return text















